package com.ruoyi.sale.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sale.domain.SaleOut;
import com.ruoyi.sale.domain.SaleOutProduct;
import com.ruoyi.sale.mapper.SaleOutMapper;
import com.ruoyi.sale.service.ISaleOutService;
import com.ruoyi.stock.domain.WarehouseOut;
import com.ruoyi.stock.domain.WarehouseOutInfo;
import com.ruoyi.stock.service.IWarehouseOutInfoService;
import com.ruoyi.stock.service.IWarehouseOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 销售出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-04
 */
@Service
public class SaleOutServiceImpl implements ISaleOutService
{
    @Autowired
    private SaleOutMapper saleOutMapper;

    @Resource
    private IWarehouseOutService warehouseOutService;

    @Resource
    private IWarehouseOutInfoService warehouseOutInfoService;

    /**
     * 查询销售出库
     * 
     * @param id 销售出库主键
     * @return 销售出库
     */
    @Override
    public SaleOut selectSaleOutById(Long id)
    {
        return saleOutMapper.selectSaleOutById(id);
    }

    /**
     * 查询销售出库列表
     * 
     * @param saleOut 销售出库
     * @return 销售出库
     */
    @Override
    public List<SaleOut> selectSaleOutList(SaleOut saleOut)
    {
        return saleOutMapper.selectSaleOutList(saleOut);
    }

    /**
     * 新增销售出库
     * 
     * @param saleOut 销售出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSaleOut(SaleOut saleOut)
    {
        //设置创建人id为登陆人id
        Long userId = SecurityUtils.getUserId();
        saleOut.setCreateBy(String.valueOf(userId));

        saleOut.setCreateTime(DateUtils.getNowDate());
        int rows = saleOutMapper.insertSaleOut(saleOut);
        //给出库单添加信息，执行出库添加
        WarehouseOut warehouseOut = new WarehouseOut();
        warehouseOut.setWarehouseOutNo(SeqNumberUtils.getXSCKNumber());
        warehouseOut.setSourceNo(saleOut.getOrderNo());
        warehouseOut.setGoodsType((long)2);
        warehouseOut.setWarehouseOutStatus((long)1);
        warehouseOut.setUserId(userId);
        System.out.println("warehouseOut = " + warehouseOut);
        warehouseOutService.insertWarehouseOut(warehouseOut);

        //给出库详情单添加信息，执行出库详情添加
        saleOut.getSaleOutProductList().forEach(item1 -> {
            WarehouseOutInfo warehouseOutInfo = new WarehouseOutInfo();
            warehouseOutInfo.setWarehouseOutId(warehouseOut.getId());
            warehouseOutInfo.setGoodsNo(item1.getProductNo());
            warehouseOutInfo.setWarehouseOutNumber(item1.getNumber());
            warehouseOutInfoService.insertWarehouseOutInfo(warehouseOutInfo);
        });


        //给销售订单产品表添加记录
        saleOut.getSaleOutProductList().forEach(item -> {
            item.setOutId(saleOut.getId());
        });
        insertSaleOutProduct(saleOut);

        return rows;
    }

    /**
     * 修改销售出库
     * 
     * @param saleOut 销售出库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSaleOut(SaleOut saleOut)
    {
        saleOut.setUpdateTime(DateUtils.getNowDate());
        saleOutMapper.deleteSaleOutProductByOutId(saleOut.getId());
        insertSaleOutProduct(saleOut);
        return saleOutMapper.updateSaleOut(saleOut);
    }

    /**
     * 批量删除销售出库
     * 
     * @param ids 需要删除的销售出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSaleOutByIds(Long[] ids)
    {
        saleOutMapper.deleteSaleOutProductByOutIds(ids);
        return saleOutMapper.deleteSaleOutByIds(ids);
    }

    /**
     * 删除销售出库信息
     * 
     * @param id 销售出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSaleOutById(Long id)
    {
        saleOutMapper.deleteSaleOutProductByOutId(id);
        return saleOutMapper.deleteSaleOutById(id);
    }

    /**
     * 新增销售出库产品信息
     * 
     * @param saleOut 销售出库对象
     */
    public void insertSaleOutProduct(SaleOut saleOut)
    {
        List<SaleOutProduct> saleOutProductList = saleOut.getSaleOutProductList();
        Long id = saleOut.getId();
        if (StringUtils.isNotNull(saleOutProductList))
        {
            List<SaleOutProduct> list = new ArrayList<SaleOutProduct>();
            for (SaleOutProduct saleOutProduct : saleOutProductList)
            {
                saleOutProduct.setOutId(id);
                list.add(saleOutProduct);
            }
            if (list.size() > 0)
            {
                saleOutMapper.batchSaleOutProduct(list);
            }
        }
    }
}
